package com.smcdd.cdb.dbo.cdb_dbo.ui.fragment;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.event.ReachEvent;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.DayShop;
import com.smcdd.cdb.dbo.cdb_dbo.service.presenter.ReachNotPresenter;
import com.smcdd.cdb.dbo.cdb_dbo.service.view.IReachNotFg;
import com.smcdd.cdb.dbo.cdb_dbo.ui.activity.DetailActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BaseFragmentFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * 作者：Administrator on 2017/7/6 13:23
 * 邮箱：zhanghuaiha@gmail.com
 * 未达标
 */

public class ReachNotFragment extends BaseFragmentFragment<IReachNotFg,ReachNotPresenter> implements IReachNotFg{
    private RecyclerView mRcycler;
    private EditText yet_search;
    private TextView yet_search_tv;
    private List<DayShop> dayShops;

    @Override
    public void initView(View rootView) {
        EventBus.getDefault().register(this);
        mRcycler = (RecyclerView) rootView.findViewById(R.id.yet_recycler);
        yet_search = (EditText) rootView.findViewById(R.id.yet_search);
        yet_search_tv = (TextView) rootView.findViewById(R.id.yet_search_tv);
    }

    @Override
    public void initData() {

    }

    @Override
    public void initListener() {
        yet_search_tv.setOnClickListener(v -> {
//            List<StoreInfo.DataBean> date = mPresenter.getDate(type);
            if (dayShops != null) {
                mPresenter.searh(yet_search_tv.getText().toString(),dayShops);
                if (yet_search_tv.getText().toString().equals("搜索")) {
                    yet_search_tv.setText("取消");
                }else {
                    yet_search_tv.setText("搜索");
                }
            }
        });

        yet_search.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (hasFocus) {
                    // 此处为得到焦点时的处理内容

                } else {
                    // 此处为失去焦点时的处理内容
                    InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
                }
            }
        });

        mRcycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(recyclerView.getApplicationWindowToken(), 0);
            }
        });
    }

    @Override
    protected ReachNotPresenter createPresenter() {
        return new ReachNotPresenter((DetailActivity)getActivity());
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_yet;
    }

    @Override
    public RecyclerView getRecycler() {
        return mRcycler;
    }

    @Override
    public DetailActivity getAc() {
        return (DetailActivity)getActivity();
    }

    @Override
    public EditText getSearch() {
        return yet_search;
    }

    //获取数据
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRefresh(ReachEvent event){
        if (event != null) {
            dayShops = event.getmNotList();
            mPresenter.loadContacts(dayShops);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
